package Cinema.Tests;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import Cinema.Client;
import Cinema.User;
import Cinema.CinemaEntities.Location;
import Cinema.CinemaEntities.Movie;
import Cinema.CinemaEntities.Site;
import Cinema.CinemaEntities.Theater;
import Cinema.DataLayer.DataManager;
import Cinema.DataLayer.DataPackage;
import Cinema.DataLayer.IDataBackup;
import Exceptions.DataAccessException;
import Exceptions.ObjectAlreadyExist;
import Exceptions.WrongXMLElement;

public class DataManagerTest {

	DataManager dataManeger;

	String password;

	DataPackage pack;

	ArrayList<Movie> movies;
	ArrayList<Site> sites;
	ArrayList<User> users;

	@Before
	public void InitTest() throws WrongXMLElement
	{
		pack = new DataPackage();

		movies = new ArrayList<Movie>();
		movies.add(new Movie("movie1", "gen1"));
		movies.add(new Movie("movie2", "gen2"));

		sites = new ArrayList<Site>();
		sites.add(new TestSite("site1",new Location(4, 5), "Ashdod"));

		users = new ArrayList<User>();
		Client temp = new Client("Ofir", "a@a.com", null);
		password = temp.getPassword();
		users.add(temp);
		users.add(new Client("Grisha", "b@b.com", null));
		users.add(new Client("Alex", "c@c.com", null));
		users.add(new Client("Itamar", "d@d.com", null));

		dataManeger = new DataManager(new DataAccess());

	}

	/**
	 * Test add user - test adding existing user
	 */
	@Test (expected = ObjectAlreadyExist.class)
	public void testAddUser_testAddingExistingUser() throws ObjectAlreadyExist {
		dataManeger.addUser(new Client("Ofir", "r@r.com", dataManeger));
	}

	/**
	 * Test add user - test adding new user
	 */
	@Test
	public void testAddUser2() throws ObjectAlreadyExist {
		dataManeger.addUser(new Client("Alaa", "r@r.com", dataManeger));
	}

	/**
	 * Test get user - get existing user
	 */
	@Test
	public void testGetUser_testGetExistingUser() {
		assertNotNull(dataManeger.getUser("Ofir", password));
	}

	/**
	 * Test get user - try to get not existing user
	 */
	@Test
	public void testGetUser_testGetNotExistingUser() {
		assertNull(dataManeger.getUser("aaaaa", password));
	}

	/**
	 * Test get user - get with wrong password
	 */
	@Test
	public void testGetUser_testWrongPassword() {
		assertNull(dataManeger.getUser("Ofir", password + "5"));
	}

	/**
	 * Test get movie getter
	 */
	@Test
	public void testGetMovies() {
		assertSame(movies, dataManeger.getMovies());
	}

	/**
	 * Test get sites getter
	 */
	@Test
	public void testGetSites() {
		assertSame(sites, dataManeger.getSites());
	}

	class DataAccess implements IDataBackup{

		@Override
		public void saveSites(ArrayList<Site> sites) {

		}

		@Override
		public void saveUsers(ArrayList<User> users) {

		}

		@Override
		public void saveMovies(ArrayList<Movie> movies) {

		}

		@Override
		public DataPackage getCinemaData(boolean isRestoring) throws DataAccessException {
			pack.movies = movies;
			pack.sites = sites;
			pack.users = users;
			return pack;
		}

	}

	class TestSite extends Site{

		protected TestSite(String siteName, Location siteLocation, String city) {
			super(siteName, siteLocation, city);
			this.city = city;
			this.location = siteLocation;
			this.name = siteName;
		}

		@Override
		public Set<Theater> getTheaters() {
			return null;
		}

	}

}
